package com.lsh.action;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.lsh.common.util.PageSurport;
import com.lsh.model.TsysUser;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author lsh
 *
 */
public class BasicAction extends ActionSupport implements ServletRequestAware,ServletResponseAware {
	
	protected String backUrlHead="test/";
	protected String base;
	protected String queryStr;
	public String pageRight = "view-edit-delete-add-deleteAll-find";
	public String orderField = "";
	public List datas;
	private static final long serialVersionUID = 1L;
	/**
	 * 返回信息
	 */
	public String message="";
	
	/**
	 * 返回地址
	 */
	public String backUrl;
	/**
	 * 访问的uri
	 */
	public String uri;
	public PageSurport page = new PageSurport();
	int pageNo = 1;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	
	public String list(){
		Enumeration a = request.getParameterNames();
		StringBuffer sb = new StringBuffer();
		int i = 0;
		while (a.hasMoreElements()) {
			String name = (String) a.nextElement();
//			System.out.println("参数："+a.nextElement());
			if(!name.equals("backUrl")){
				String value=request.getParameter(name);
				if(name!=null && !name.equals("") && value!=null && !value.equals("")){
					if(i>0){
						sb.append("&"+name+"="+value);
					}else{
						sb.append("?"+name+"="+value);
					}
					i=i+1;
				}
			}
		}
		backUrl=sb.toString();
		return "";
				
	}
	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
		uri=request.getRequestURI();
		System.out.println("sessionID:"+request.getRequestedSessionId());
		System.out.println("queryString:"+request.getQueryString());
		System.out.println("IP:"+request.getRemoteAddr());
		System.out.println("uri:"+uri);
		
		Enumeration<String> enums = request.getParameterNames();
		while (enums.hasMoreElements()) {
			String name = enums.nextElement();
			System.out.println("参数："+name+"值："+request.getParameter(name));
			
		}
		
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response=response;
	}
	
	public Object get(String name){
		HttpSession session = request.getSession();
		return session.getAttribute(name);
	}
	public void setSession(String key, Object object) {
		HttpSession session = request.getSession();
		session.setAttribute(key, object);
	}
	public PageSurport getPage() {
		return page;
	} 

	public void setPage(PageSurport page) {
		this.page = page;
	}

	public String getMessage() {
		return message;
	}

	public String getBackUrl() {
		return backUrl;
	}

	public ArrayList<String> getRights() {
		String aa[] =  pageRight.split("-");
		ArrayList<String> list = new ArrayList<String>();
		for(String a: aa){
			list.add(a);
		}
		return list;
	}

	public String getPageRight() {
		return pageRight;
	}

	public String getOrderField() {
		return orderField;
	}

	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	public String getBase() {
		return request.getContextPath();
	}

	public String getQueryStr() {
		return this.queryStr;
	} 

	public void setQueryStr(String queryStr) {
		this.queryStr = queryStr;
	}


	public String getBackUrlHead() {
		return backUrlHead;
	}
	public void setBackUrl(String backUrl) {
		this.backUrl = backUrl;
	}
	public List getDatas() {
		return datas;
	}

}
